<?php

include_once(APP_URL.'protected/lib/private/Check.php');
include_once(APP_URL.'protected/lib/private/Str.php');

class BrowserController extends BaseController {

    public function actionIndex() {
        // http://192.168.3.69/painter/index

    }

    public function actionIntro() {
        // /browser/intro
        $this->renderPartial('browser_intro');
    }

    public function actionBrowser() {
        // http://192.168.3.69/painter/browser

        $projectId = Check::checkGetParam(0, 'projectId', false);

        // get project name
        $condArr = array(
            'select' => array('*'),
            'condition' => 'id=:projectId',
            'params' => array('projectId' => $projectId)
        );
        $TblProject = new TblProject();
        $result = $TblProject->selectRecord($condArr);
        if(!$result) {
            echo Resp::retFail(0, 'find project failed');
            exit;
        }
        $projectName = $result->name;
        $mapUrl = $result->map_url;

        // get project logo & music
        $logoUrl = null;
        $musicUrl = null;
        $condArr = array(
            'select' => array('*'),
            'condition' => 'project_id=:projectId AND type=:type',
            'params' => array('projectId' => $projectId, 'type' => TYPE_FILE_LOGO)
        );
        $TblProjectFile = new TblProjectFile();
        $result = $TblProjectFile->selectRecord($condArr);
        if($result) {
            $logoUrl = URL_FILE_ROOT.$projectId.'/'.basename($result->url);
        }
        $condArr = array(
            'select' => array('*'),
            'condition' => 'project_id=:projectId AND type=:type',
            'params' => array('projectId' => $projectId, 'type' => TYPE_FILE_MUSIC)
        );
        $result = $TblProjectFile->selectRecord($condArr);
        if($result) {
            $musicUrl = URL_FILE_ROOT.$projectId.'/'.basename($result->url);
        }

        $arr = array(
            'projectId' => $projectId,
            'projectName' => $projectName,
            'logoUrl' => $logoUrl,
            'musicUrl' => $musicUrl,
            'mapUrl' => $mapUrl,
            'prjUrl' => '/browser/getPrj/projectId/'.$projectId,
        );

        $agent = strtolower($_SERVER['HTTP_USER_AGENT']);
        $pc = (strpos($agent, 'windows nt')) ? true : false;
        $iPhone = (strpos($agent, 'iphone')) ? true : false;
        $iPad = (strpos($agent, 'ipad')) ? true : false;
        $android = (strpos($agent, 'android')) ? true : false;

        if($pc) {
            $this->renderPartial('browser_pc', $arr);
        } else if($android) {
            $this->renderPartial('browser_android', $arr);
        } else if($iPhone) {
            $this->renderPartial('browser_iPhone', $arr);
        } else if($iPad) {
            $this->renderPartial('browser_iPad', $arr);
        }
    }

    public function actionGetConf() {
        // http://192.168.3.69/browser/getConf

        $projectId = Check::checkGetParam(0, 'projectId', false);

        $condArr = array(
            'select' => array('path_url'),
            'condition' => 'id=:projectId ',
            'params' => array('projectId' => $projectId)
        );
        $TblProject = new TblProject();
        $result = $TblProject->selectRecord($condArr);
        if(!$result) {
            echo Resp::retFail(0, 'find project failed');
            exit;
        }
        $pathUrl = $result->path_url;
        if(!is_file($pathUrl)) {
            echo Resp::retFail(0, 'conf file not exist');
            exit;
        }

        $pathData = file_get_contents($pathUrl);
        if(!$pathData) {
            echo Resp::retFail(0, 'read conf file failed');
            exit;
        }
        echo $pathData;
    }

    public function actionGetPrj() {
        // http://192.168.3.69/browser/getPrj

        $projectId = Check::checkGetParam(0, 'projectId', false);

        $condArr = array(
            'select' => array('prj_url'),
            'condition' => 'id=:projectId',
            'params' => array('projectId' => $projectId)
        );
        $TblProject = new TblProject();
        $result = $TblProject->selectRecord($condArr);
        if(!$result) {
            echo Resp::retFail(0, 'find project failed');
            exit;
        }
        $prjUrl = $result->prj_url;
//        echo $prjUrl;exit;

        $data = file_get_contents($prjUrl);
        if(!$data) {
            echo Resp::retFail(0, 'read prj file failed');
            exit;
        }
//        echo $data;exit;

        $dataObj = null;
        $dataObj = json_decode($data);
        if(!$dataObj) {
            echo Resp::retFail(0, 'decode prj file failed');
            exit;
        }
//        echo var_dump($dataObj);exit;
        $prjObj = $dataObj[6][1];
        $prjArr = [];
        foreach($prjObj as $value) {
            foreach($value[4] as $key=>$val) {
                $value[4][$key] = $val + Str::randomFloat();
            }
            $prjArr[] = array($value[1], $value[2], $value[4], Str::randomFloat());
        }

        $data = json_encode(array($dataObj[0], $dataObj[1], $dataObj[2], $dataObj[3],$dataObj[4], $dataObj[5], array($dataObj[6][0], $prjArr)));
        echo $data;
    }

}
